package hn.sigit.logic.viewer.toolbox;

import hn.sigit.dao.hnd.cadastre.HND_ParcelDAO;
import hn.sigit.dao.hnd.cadastre.HND_SpatialZoneDAO;
import hn.sigit.dao.hnd.ladmshadow.ParcelDAO;
import hn.sigit.dao.hnd.ladmshadow.SpatialZoneDAO;
import hn.sigit.logic.viewer.InteractiveViewerHelper;
import hn.sigit.model.commons.IEasement;
import hn.sigit.model.commons.IImprovement;
import hn.sigit.model.commons.IParcel;
import hn.sigit.model.commons.ISpatialZone;
import hn.sigit.model.hnd.cadastre.HND_Easement;
import hn.sigit.model.hnd.cadastre.HND_Improvement;
import hn.sigit.model.hnd.cadastre.HND_Parcel;
import hn.sigit.model.hnd.cadastre.HND_SpatialZone;
import hn.sigit.model.hnd.ladmshadow.Easement;
import hn.sigit.model.hnd.ladmshadow.Improvement;
import hn.sigit.model.hnd.ladmshadow.Parcel;
import hn.sigit.model.hnd.ladmshadow.SpatialZone;
import hn.sigit.util.EditMode;

import java.io.Serializable;


public class ParcelDataHelper implements Serializable {
	private static final long serialVersionUID = 1L;

	private InteractiveViewerHelper interactiveViewerHelper;
	
	private IImprovement improvement;
	private EditMode improvementEditMode = EditMode.VIEWING;
	
	private IEasement easement;
	private EditMode easementEditMode = EditMode.VIEWING;

	private boolean editing = false;
	

	public ParcelDataHelper(InteractiveViewerHelper interactiveViewerHelper) {
		this.interactiveViewerHelper = interactiveViewerHelper;
	}
	
	public ISpatialZone getSelectedZone() {
		return interactiveViewerHelper.getSelectedZone();
	}
	
	public IImprovement getImprovement() {
		return improvement;
	}
	public void setImprovement(IImprovement improvement) {
		this.improvement = improvement;
	}
	public EditMode getImprovementEditMode() {
		return improvementEditMode;
	}


	public IEasement getEasement() {
		return easement;
	}
	public void setEasement(IEasement easement) {
		this.easement = easement;
	}
	public EditMode getEasementEditMode() {
		return easementEditMode;
	}


	public String newImprovement() {
		improvementEditMode = EditMode.ADDING;
		if (getSelectedZone() instanceof Parcel)
			improvement = new Improvement();
		else if (getSelectedZone() instanceof HND_Parcel)
			improvement = new HND_Improvement();
		return null;
	}
	public String editImprovement(IImprovement improvement) {
		improvementEditMode = EditMode.EDITING;
		this.improvement = improvement;
		return null;
	}
	public String viewImprovement(IImprovement improvement) {
		improvementEditMode = EditMode.VIEWING;
		this.improvement = improvement;
		return null;
	}
	public String acceptImprovement() {
		switch (improvementEditMode) {
		case ADDING:
			interactiveViewerHelper.getSelectedParcel().getImprovements().add(improvement);
			break;
		case DELETING:
			interactiveViewerHelper.getSelectedParcel().getImprovements().remove(improvement);
			break;
		default:
			break;
		}
		return null;
	}

	public String newEasement() {
		easementEditMode = EditMode.ADDING;
		if (getSelectedZone() instanceof Parcel)
			easement = new Easement();
		else if (getSelectedZone() instanceof HND_Parcel)
			easement = new HND_Easement();
		return null;
	}
	public String editEasement(IEasement easement) {
		easementEditMode = EditMode.EDITING;
		this.easement = easement;
		return null;
	}
	public String viewEasement(IEasement easement) {
		easementEditMode = EditMode.VIEWING;
		this.easement = easement;
		return null;
	}
	public String acceptEasement() {
		if (getSelectedZone() instanceof IParcel) {
			switch (easementEditMode) {
			case ADDING:
				((IParcel) getSelectedZone()).getEasements().add(easement);
				break;
			case DELETING:
				((IParcel) getSelectedZone()).getEasements().remove(easement);
				break;
			default:
				break;
			}
		}

		return null;
	}
	

	public String beginEditData() {
		editing = true;
		return null;
	}
	
	public String cancelEditData() {
		editing = false;
		return null;
	}
	
	public String applyDataChange() {
		if (getSelectedZone() != null) {
			if (getSelectedZone() instanceof Parcel) {
				((Parcel) getSelectedZone()).setModified(true);
				((Parcel) getSelectedZone()).setOriginal(false);
				ParcelDAO.save(getSelectedZone());
			}
			else if (getSelectedZone() instanceof HND_Parcel) {
				HND_ParcelDAO.save(getSelectedZone());
			}
			else if (getSelectedZone() instanceof SpatialZone) {
				((SpatialZone) getSelectedZone()).setModified(true);
				((SpatialZone) getSelectedZone()).setOriginal(false);
				SpatialZoneDAO.save(getSelectedZone());
			}
			else if (getSelectedZone() instanceof HND_SpatialZone) {
				HND_SpatialZoneDAO.save(getSelectedZone());
			}
			editing = false;
		}
		return null;
	}

	public boolean isEditing() {
		return editing;
	}
	
	public String getPrimaryLandUseName() {
		if (getSelectedZone() == null
				|| getSelectedZone().getPrimaryLandUse() == null)
			return "";
		
		return getSelectedZone().getPrimaryLandUse().getFullName();
	}
	public void setPrimaryLandUseName(String landUseName) {
	}
	
	public String getSecondaryLandUseName() {
		if (getSelectedZone() == null || getSelectedZone().getSecondaryLandUse() == null)
			return "";
		return getSelectedZone().getSecondaryLandUse().getFullName();
	}
	public void setSecondaryLandUseName(String landUseName) {
	}

}
